package view.component.asset
{
import flash.display.MovieClip;

import framework.core.AssetsManager;
import framework.proxy.ComponentProxy;
import framework.view.components.graphics.Asset;

/**
 * IconProxy
 * @author jliang
 *
 */
public class IconProxy extends ComponentProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function IconProxy()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var _url:String;

	public function get url():String
	{
		return _url;
	}
	
	protected var _onComplete:Function;

	public function get onComplete():Function
	{
		return _onComplete;
	}
	
	protected var _asset:Asset;

	public function get asset():Asset
	{
		return _asset;
	}

	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override public function dispose():void
	{
		stop();
		
		super.dispose();		
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function load(url:String, onComplete:Function = null):void
	{
		stopAndRemove();
		
		this._url = url;
		this._onComplete = 	onComplete;	
		
		_asset = AssetsManager.loadAsset(url, loadedHandler, errorHandler);
	}
	
	public function stop():void
	{
		if (asset)
		{
			asset.dispose();
			_asset = null;
		}
		
		_url = null;
		_onComplete = null;
	}
	
	public function stopAndRemove():void
	{
		if (asset && asset.content && asset.content.parent)
		{
			asset.content.parent.removeChild(asset.content);
		}
		stop();
	}
	
	protected function loadedHandler(asset:Asset):void
	{
		skin["assetHolder"].addChild(asset.content);
		
		if (onComplete != null)
		{
			onComplete(this);
		}
	}
	
	protected function errorHandler(asset:Asset):void
	{
		
	}
}
}



